package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.MassifHole;
import com.lsyw.intergrate.entity.MassifUser;
import com.lsyw.intergrate.entity.TransportPlan;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.MassifHoleService;
import com.lsyw.intergrate.service.TransportPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输计划 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-27
 */
@Api(tags = "运输计划")
@RestController
@RequestMapping("/transport-plan")
public class TransportPlanController {


    @Autowired
    private TransportPlanService transportPlanService;
    @Autowired
    private MassifHoleService massifHoleService;

    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        TransportPlan transportPlan = transportPlanService.getById(id);
        String holeIds = transportPlan.getHoleIds();

        List<MassifHole> holeList = new ArrayList<>();
        if (!StringUtils.isEmpty(holeIds)) {
            String[] split = holeIds.split(",");
            for (String holeId : split) {
                MassifHole massifHole = this.massifHoleService.getById(holeId);
                holeList.add(massifHole);
            }
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("transportPlan",transportPlan);
        map.put("holeList",holeList);
        return Result.successResult(map);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody TransportPlan transportPlan) {
        transportPlanService.save(transportPlan);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        TransportPlan transportPlan = transportPlanService.getById(Long.parseLong(id));
        transportPlan.setDelFlag(1);
        this.transportPlanService.updateById(transportPlan);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody TransportPlan transportPlan) {
        transportPlanService.updateById(transportPlan);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody TransportPlanReqVo transportPlan) {
        final LambdaQueryWrapper<TransportPlan> lambda = new QueryWrapper<TransportPlan>().lambda();
        this.buildCondition(lambda, transportPlan);
        return Result.successResult(transportPlanService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<TransportPlan> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<TransportPlan> lambda = new QueryWrapper<TransportPlan>().lambda();
        lambda.eq(TransportPlan::getDelFlag,0);
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(TransportPlan::getName, condition.get("name"));
        }
        if (condition.get("massifId") != null && !StringUtils.isEmpty(condition.get("massifId").toString())) {
            lambda.eq(TransportPlan::getMassifId, condition.get("massifId"));
        }
        Page<TransportPlan> page1 = this.transportPlanService.page(page, lambda);
        List<TransportPlan> records = page1.getRecords();
        for (int i = 0; i < records.size(); i++) {
            List<MassifHole> massifHoles = new ArrayList<>();
            String holeIds = records.get(i).getHoleIds();
            if (!StringUtils.isEmpty(holeIds)){
                String[] split = holeIds.split(",");
                for (String holeId:split) {
                    MassifHole massifHole = massifHoleService.getById(holeId);
                    massifHoles.add(massifHole);
                }

            }

            records.get(i).setMassifHoles(massifHoles);
        }
        page1.setRecords(records);
        return Result.successResult(page1);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<TransportPlan> lambda, TransportPlanReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(TransportPlan::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(TransportPlan::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getHoleIds())) {
            lambda.eq(TransportPlan::getHoleIds, param.getHoleIds());
        }
        if (!StringUtils.isEmpty(param.getWeight())) {
            lambda.eq(TransportPlan::getWeight, param.getWeight());
        }
        if (!StringUtils.isEmpty(param.getModeTransport())) {
            lambda.eq(TransportPlan::getModeTransport, param.getModeTransport());
        }
        if (!StringUtils.isEmpty(param.getRouteId())) {
            lambda.eq(TransportPlan::getRouteId, param.getRouteId());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(TransportPlan::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(TransportPlan::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(TransportPlan::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(TransportPlan::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(TransportPlan::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(TransportPlan::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(TransportPlan::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(TransportPlan::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, TransportPlan::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class TransportPlanReqVo extends TransportPlan {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
